from apps.pay.lzpay.baseViews import *
from project.settings import BASE_DIR
from apps.interface.client.models import Client
from apps.pay.order.dao import OrderDao

class LzPayAcctViewSet(BaseLzpayViewSet):
    
    ''''账户信息查询'''
    @action(methods=['post'], detail=False)
    def queryAccountInfo(self, request):
        params = getRequestParams(request)
        r_params =  {
            "signType": self.signType,
            "signSN": self.signSN,
            "encryptSN": self.encryptSN,
            "merchantNo": self.merchantNo
        }
        r_params.update({
            "subMerchantNo": params.get('subMerchantNo')
        })
        sign = self.getSign(r_params)
        if sign:
            r_params['sign'] = sign
        else:
            return Response(response_message('获取签名失败'))
        # 输出程序输出
        url = "https://acct.lzpay.com/api/query/queryAccountInfo"
        headers = {'content-type': "application/x-www-form-urlencoded;charset=UTF-8"}
        print('r_params', r_params)
        res = requests.post(url, data=r_params, headers=headers)
        res_content = json.loads(res.content)
        print('res_content', res_content)
        if res_content.get('code') == '00':
            data = json.loads(res_content.get('resultJsonString'))
            return Response(response_success(data))
        else:
            return Response(response_message(res_content.get('message')))
        
    ''''账户余额查询'''
    @action(methods=['post'], detail=False)
    def acctBalance(self, request):
        params = getRequestParams(request, False)
        r_params =  {
            "signType": self.signType,
            "signSN": self.signSN,
            "encryptSN": self.encryptSN,
            "merchantNo": self.merchantNo
        }
        r_params.update({
            "accountType": 0
        })
        sign = self.getSign(r_params)
        if sign:
            r_params['sign'] = sign
        else:
            return Response(response_message('获取签名失败'))
        # 输出程序输出
        url = "https://acct.lzpay.com/api/merchant/acctBalance"
        headers = {'content-type': "application/x-www-form-urlencoded;charset=UTF-8"}
        print('r_params', r_params)
        res = requests.post(url, data=r_params, headers=headers)
        res_content = json.loads(res.content)
        print('res_content', res_content)
        if res_content.get('code') == '00':
            return Response(response_success(res_content))
        else:
            return Response(response_message(res_content.get('message')))
        
    ''''账户注册发送短信'''
    @action(methods=['post'], detail=False)
    def registerSmsCode(self, request):
        params = getRequestParams(request, False)
        r_params =  {
            "signType": self.signType,
            "signSN": self.signSN,
            "encryptSN": self.encryptSN,
            "merchantNo": self.merchantNo
        }
        r_params.update({
            "merType": params.get('merType'),
            "contactName": params.get('contactName'),
            "contactEmail": params.get('contactEmail'),
            "contactIdType": params.get('contactIdType'),
            "customerNo": params.get('customerNo')
        })
        # 加密身份证和手机号
        encrypt_data = self.encrypt(params.get('contactIdNo'), params.get('contactPhone'))
        r_params["contactIdNo"] = encrypt_data.get('data1')
        r_params["contactPhone"] = encrypt_data.get('data2')
        r_params["digitalEnvelope"] = encrypt_data.get('digitalEnvelope')

        sign = self.getSign(r_params)
        if sign:
            r_params['sign'] = sign
        else:
            return Response(response_message('获取签名失败'))
        # 输出程序输出
        url = "https://acct.lzpay.com/api/merchant/registerSmsCode"
        headers = {'content-type': "application/x-www-form-urlencoded;charset=UTF-8"}
        print('r_params', r_params)
        res = requests.post(url, data=r_params, headers=headers)
        res_content = json.loads(res.content)
        print('res_content', res_content)
        if res_content.get('code') == '00':
            return Response(response_message(res_content.get('message')))
        else:
            return Response(response_message(res_content.get('message')))
    
    ''''账户快捷注册'''
    @action(methods=['post'], detail=False)
    def registerSpeedy(self, request):
        params = getRequestParams(request, False)
        r_params =  {
            "signType": self.signType,
            "signSN": self.signSN,
            "encryptSN": self.encryptSN,
            "merchantNo": self.merchantNo
        }
        r_params.update({
            "merType": params.get('merType'),
            "contactName": params.get('contactName'),
            "contactIdNo": params.get('contactIdNo'),
            "contactPhone": params.get('contactPhone'),
            "contactEmail": params.get('contactEmail'),
            "contactIdType": params.get('contactIdType'),
            "customerNo": params.get('customerNo'),
            "smsCode": params.get('smsCode')
        })
        # 加密身份证和手机号
        encrypt_data = self.encrypt(params.get('contactIdNo'), params.get('contactPhone'))
        r_params["contactIdNo"] = encrypt_data.get('data1')
        r_params["contactPhone"] = encrypt_data.get('data2')
        r_params["digitalEnvelope"] = encrypt_data.get('digitalEnvelope')
        sign = self.getSign(r_params)
        if sign:
            r_params['sign'] = sign
        else:
            return Response(response_message('获取签名失败'))
        # 输出程序输出
        url = "https://acct.lzpay.com/api/merchant/registerSpeedy"
        headers = {'content-type': "application/x-www-form-urlencoded;charset=UTF-8"}
        # print('r_params', r_params)
        res = requests.post(url, data=r_params, headers=headers)
        res_content = json.loads(res.content)
        print('res_content', res_content)
        if res_content.get('code') == '00':
            return Response(response_message(res_content.get('message')))
        else:
            return Response(response_message(res_content.get('message')))
        
    ''''账户注册图片上传'''
    @action(methods=['post'], detail=False)
    def uploadImage(self, request):
        params = getRequestParams(request, False)
        if not request.FILES.get('file'):
            return Response(response_message('文件为空'))
        fp = request.FILES.get('file')
        # 读取文件内容
        file_content = fp.read()
        # 将文件内容编码base64编码
        base64_encoded_content = base64.b64encode(file_content).decode('utf-8')


        r_params =  {
            "signType": self.signType,
            "signSN": self.signSN,
            "encryptSN": self.encryptSN,
            "merchantNo": self.merchantNo
        }
        r_params.update({
            "subMerchantNo": params.get('subMerchantNo'),
            "image": base64_encoded_content
            })
        sign = self.getSign(r_params)
        if sign:
            r_params['sign'] = sign
        else:
            return Response(response_message('获取签名失败'))
        # 输出程序输出
        url = "https://acct.lzpay.com/api/merchant/uploadImage"
        headers = {'content-type': "application/x-www-form-urlencoded;charset=UTF-8"}
        # print('r_params', r_params)
        res = requests.post(url, data=r_params, headers=headers)
        res_content = json.loads(res.content)
        print('res_content', res_content)
        if res_content.get('code') == '00':
            return Response(response_success(res_content.get('fileId')))
        else:
            return Response(response_message(res_content.get('message')))
        
    ''''账户注册认证'''
    @action(methods=['post'], detail=False)
    def registerAuth(self, request):
        params = getRequestParams(request, False)
        r_params =  {
            "signType": self.signType,
            "signSN": self.signSN,
            "encryptSN": self.encryptSN,
            "merchantNo": self.merchantNo
        }
        r_params.update({
            "subMerchantNo": params.get('subMerchantNo'),
            "merchantCategory": params.get('merchantCategory'),
            "mcc": params.get('mcc'),
            "legalIDCardStart": params.get('legalIDCardStart'),
            "legalIDCardValidity": params.get('legalIDCardValidity'),
            "legalIDCardAvatar": params.get('legalIDCardAvatar'),
            "legalIDCardEmblem": params.get('legalIDCardEmblem')
        })
        sign = self.getSign(r_params)
        if sign:
            r_params['sign'] = sign
        else:
            return Response(response_message('获取签名失败'))
        # 输出程序输出
        url = "https://acct.lzpay.com/api/merchant/registerAuth"
        headers = {'content-type': "application/x-www-form-urlencoded;charset=UTF-8"}
        # print('r_params', r_params)
        res = requests.post(url, data=r_params, headers=headers)
        res_content = json.loads(res.content)
        print('res_content', res_content)
        if res_content.get('code') == '00':
            return Response(response_message(res_content.get('message')))
        else:
            return Response(response_message(res_content.get('message')))
        
    
    
